package com.project.hqshorttaxi.service.impl;

import com.project.hqshorttaxi.dto.WhiteListInputParam;
import com.project.hqshorttaxi.entity.WhiteListInput;
import com.project.hqshorttaxi.mapper.WhiteListInputMapper;
import com.project.hqshorttaxi.service.WhiteListInputService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WhiteListInputServiceImpl implements WhiteListInputService {

    @Resource
    private WhiteListInputMapper whiteListInputMapper;

    @Override
    public WhiteListInput getById(Integer id) {
        return whiteListInputMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(WhiteListInput entity) throws Exception {
        whiteListInputMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(WhiteListInput entity) throws Exception {
        whiteListInputMapper.updateByPrimaryKeySelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer id) throws Exception {
        whiteListInputMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<WhiteListInput> list(WhiteListInputParam param) {
        Example example = new Example(WhiteListInput.class);
        Example.Criteria c = example.createCriteria();
        if (!StringUtils.isEmpty(param.getPlateNumber())) {
            c.andLike("plateNumber", "%" + param.getPlateNumber() + "%");
        }
        if (param.getStatus() != null) {
            c.andEqualTo("status", param.getStatus());
        }
        if (param.getRejectBeginTime() != null) {
            c.andGreaterThanOrEqualTo("rejectTime", param.getRejectBeginTime());
        }
        if (param.getRejectEndTime() != null) {
            c.andLessThanOrEqualTo("rejectTime", param.getRejectEndTime());
        }
        if (param.getBeginTime() != null) {
            c.andGreaterThanOrEqualTo("createTime", param.getBeginTime());
        }
        if (param.getEndTime() != null) {
            c.andLessThanOrEqualTo("createTime", param.getEndTime());
        }
        example.orderBy("createTime").desc();
        example.orderBy("rejectTime").asc();
        List<WhiteListInput> list = whiteListInputMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<WhiteListInput> listByPlateAndDate(String plateNumber, String date) {
        Example example = new Example(WhiteListInput.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("plateNumber", plateNumber);
        c.andEqualTo("rejectDate", date);
        List<WhiteListInput> list = whiteListInputMapper.selectByExample(example);
        return list;
    }
}
